MsgN("-- player.lua loaded")

local _ply = FindMetaTable("Player")

--holds all the players skills
_ply.Skills = {}

--holds all the items the person is carrying (seeds, water, food, tools, etc)
_ply.Inventory = {}

--holds a count system for how many items the player has spawned (example: Tress)
_ply.Counts = {}


_ply.Sleepiness = 0
_ply.Thirst = 0
_ply.Hunger = 0
_ply.Stamina = 0



--[[
	Player Actions System
]]

--send player to sleep
function _ply:DoSleep()
	-- send player a sleep message
	self:EasyMessage( "You are now asleep!", 5 )
	self:SetNWBool( "Awake", true )
	self.LastSleep = CurTime()

	--[[
		Todo:
		Replace freeze with a nice animation of player laying down (ragdoll?)
	]]
	self:Freeze( true )

	timer.Simple(5, function()
		self:DoWakeup()
	end)
end

--wake player up if asleep
function _ply:DoWakeup()
	-- send player a sleep message
	self:EasyMessage( "You are now awake!", 5 )
	self:SetNWBool( "Awake", false )
	self.LastSleep = CurTime()

	--[[
		Todo:
		Replace unfreeze with a nice animation of player standing up
	]]
	self:Freeze( false )
end


--[[
	Player Skills System
]]

--gets the players skill level
function _ply:GetSkill( name )
	return (self.Skills[name] or 0)
end

--adds a new skill completely. "level" is optional. default is 0 (zero)
function _ply:AddSkill( name, level )
	self.Skills[name] = math.Clamp((self.Skills[name] or 0) + (level or 1), 0, 10)
	self:SetNWInt("Skill"..name, self.Skills[name])
end

function _ply:SetSkill( name, value )
	self.Skills[name] = math.Clamp(value, 0, 10)
	self:SetNWInt("Skill"..name, self.Skills[name])
end

--removes a skill completely
function _ply:RemoveSkill( name )
	self.Skills[name] = nil
	self:SetNWInt("Skill"..name, 0)
end

--Increases a skil. "levels" is optional (default 1)
function _ply:IncreaseSkill( name, levels )
	self.Skills[name] = (self.Skills[name] or 0) + (levels or 1)
	self:SetNWInt("Skill"..name, self.Skills[name])
end

--Decreases a skil. "levels" is optional (default 1)
function _ply:DecreaseSkill( name, levels )
	self.Skills[name] = math.Clamp((self.Skills[name] or 0) - (levels or 1), 0, 99999)
	self:SetNWInt("Skill"..name, self.Skills[name])
end


--[[
	Player Inventory System
]]

function _ply:HasInventory( name )
	if ((self.Inventory[name] or 0) > 0) then
		return true
	else
		return false
	end
end

function _ply:AddInventory( name, amount )
	--increase count
	self.Inventory[name] = (self.Inventory[name] or 0) + amount
	self:SetNWInt("Inventory"..name, self.Inventory[name])
end

function _ply:SetInventory( name, amount )
	--increase count
	self.Inventory[name] = amount
	self:SetNWInt("Inventory"..name, self.Inventory[name])
end

function _ply:RemoveInventory( name, amount )
	--decrease inventory and make sure we dont go below zero
	self.Inventory[name] = math.Clamp((self.Inventory[name] or 0) - (amount or 1), 0, 999999)
	self:SetNWInt("Inventory"..name, self.Inventory[name])
end


--[[
	Player Count System (lets us know how many items the player has spawned)
]]

function _ply:AddItemCount( name, value )
	self.Counts[name] = (self.Counts[name] or 0) + (value or 1)
end


function _ply:SetItemCount( name, value )
	self.Counts[name] = value
end

function _ply:RemoveItemCount( name, value )
	self.Counts[name] = math.Clamp((self.Counts[name] or 0) - (value or 1), 0, 9999999)
end

function _ply:GetItemCount( name )
	return (self.Counts[name] or 0)
end



function _ply:Process( name, time, data )
	if (self.InProcess) then
		return self:EasyMessage( "You can not start a new process." )
	end

	self.InProcess = true

	local process = table.Copy( MDRP.Processes[name] )

	for _name, _value in pairs( data or {} ) do
		process[_name] = _value
	end

	process.Owner = self
	process.Time = time

	--start process
	process:Start()

	--create a timer to kill the process after its set time
	timer.Simple( time, function( ply )
		process:Stop()
		ply.InProcess = false
	end, self )
end

--saves a players account
function _ply:Save()
	local tbl = {
		skills = self.Skills,
		inventory = self.Inventory,
		counts = self.Counts
	}

	MsgN("\nself.Inventory:")
	PrintTable(self.Inventory)
	MsgN("")

	file.Write( "mdrp/saves/"..self:UniqueID()..".txt", util.TableToKeyValues(tbl) )
end

--loads a players account
function _ply:Load()
	--Character loading
	if !file.Exists("mdrp/saves/"..self:UniqueID()..".txt") then return end
	
	local tbl = util.KeyValuesToTable(file.Read("mdrp/saves/"..self:UniqueID()..".txt"))

	for k,v in pairs( tbl["skills"] or {} ) do
		self:SetSkill(string.Capitalize(k),v)
	end

	for k,v in pairs( tbl["inventory"] or {} ) do
		self:SetInventory(string.Capitalize(k),v)
	end

	for k,v in pairs( tbl["counts"] or {}) do
		self:SetItemCount(string.Capitalize(k),v)
	end
end